package evaluation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;

public class FolderGeneration {

	private static Set<String> toProcess;

	public static void main(String[] args) throws IOException {
	
		String[] prefixes = {"added","changed","removed","total"};
		
		String[] names = {/*"test",*/"training"/*,"validation"*/};
		
		String[] techniques = {/*"DEPTA2","WISH","MY_WISH"*/"TEDWL"};
		
		for (int tec = 0; tec < techniques.length; tec++) {
			
			String technique = techniques[tec];
			
			for (int nam = 0; nam < names.length; nam++) {
				
				String name = names[nam];
				
				toProcess = loadToProcess("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/" + name);
				
				File folder = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/SizeSortedList/" + name + "/");
				
				File outp = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/Evaluation/");
				
				File from = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/",technique + "/" + name +"/");
				
				File[] files = folder.listFiles(new FileFilter() {
					
					@Override
					public boolean accept(File pathname) {
						
						return toProcess.contains(pathname.getName().replace(".list", ""));
						
					}
					
				});
				
				for (int pref = 0; pref < prefixes.length; pref++) {

					for (int i = 0; i < files.length; i++) {
						
						System.out.println("FILE: " + files[i].getName());
						
						String foldName = files[i].getName().replace(".list", "");
						
						BufferedReader br = new BufferedReader(new FileReader(files[i]));
						
						String line;
						
						int index = 0;
						
						while((line=br.readLine())!=null){
							
							File storeIn = new File(outp, name + "/" + technique + "/" + prefixes[pref] + "/" + index + "/");
							
							storeIn.mkdirs();
							
							if (copy(new File(from,foldName + "/" + prefixes[pref] + "." + line), new File(storeIn,foldName + ".html")))
								index++;
							
						}
						
					}

					
				}
				
				
			}
			
		}
		
		
		
		
	}
	
	private static boolean copy(File file, File file2){
		
		try {
			FileUtils.copyFile(file, file2);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
		
	}

	private static Set<String> loadToProcess(String fileName) throws IOException {
		
		Set<String> set = new HashSet<String>();
		
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		
		String line;
		
		while((line=br.readLine())!=null){
			
			set.add(line);
			
		}
		
		br.close();
		
		return set;
		
	}
}
